let mongoose = require('mongoose');


let schema = new mongoose.Schema({
    channel:{
        type:String,
        required: true,
        index: {
            unique: true
        }
    },
    maxMessageCount:{
        type:Number,
        default:50
    },
    members : [String],
    lastGetTick : {
        type:[Number],
        default : [],
    },
    messages:[require('./chat.message')]
},{versionKey:false});

schema.statics = {
    async findOrCreate(channel){
        let chat=await this.findOne({channel:channel});
        if(!chat){
            chat=await this.create({
                channel:channel
            })
        }
        if(chat.channel.indexOf('private')>=0 && chat.lastGetTick.length < 2){
            chat.lastGetTick = [0,0];
        }
        return chat;
    }
};
module.exports = schema